package product

import (
	"API/client"
	"API/request"
	"API/response"
	"common/utils"
	"github.com/gin-gonic/gin"
	product "product_server/proto"
)

// StartBargain 用户发起砍价
func StartBargain(c *gin.Context) {
	var req request.StartBargainRequest
	if err := c.ShouldBind(&req); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}
	userId := c.GetUint("userId")
	res, err := client.StartBargain(c, &product.StartBargainRequest{
		UserId:     int64(userId),
		ActivityId: req.ActivityId,
		ProductId:  req.ProductId,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "砍价发起", map[string]interface{}{"car": res})
}

// HelpBargain 用户帮砍
func HelpBargain(c *gin.Context) {
	var req request.HelpBargainRequest
	if err := c.ShouldBind(&req); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}
	userId := c.GetUint("userId")
	res, err := client.HelpBargain(c, &product.HelpBargainRequest{
		UserId:  int64(userId),
		OrderId: req.OrderId,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "用户帮砍", map[string]interface{}{"car": res})
}

// QueryBargainProgress 用户帮砍
func QueryBargainProgress(c *gin.Context) {
	var req request.QueryBargainProgressRequest
	if err := c.ShouldBind(&req); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}
	res, err := client.QueryBargainProgress(c, &product.QueryBargainProgressRequest{
		OrderId: req.OrderId,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "查询砍价进度", map[string]interface{}{"car": res})
}

// BargainSettlement 用户帮砍
func BargainSettlement(c *gin.Context) {
	var req request.BargainSettlementRequest
	if err := c.ShouldBind(&req); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}
	userId := c.GetUint("userId")
	res, err := client.BargainSettlement(c, &product.BargainSettlementRequest{
		UserId:  int64(userId),
		OrderId: req.OrderId,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "查询砍价进度", map[string]interface{}{"car": res})
}

// BargainProductList 砍价商品列表展示
func BargainProductList(c *gin.Context) {
	var req request.SpikesProductListRequest
	if err := c.ShouldBind(&req); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}

	res, err := client.BargainProductList(c, &product.BargainProductListRequest{
		Pages:       req.Pages,
		PagePerNums: req.PagePerNums,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "砍价商品列表展示", map[string]interface{}{"BargainProductList": res})
}

// BargainProductInfo 砍价商品详情展示
func BargainProductInfo(c *gin.Context) {
	var req request.ProductInfoRequest
	if err := c.ShouldBind(&req); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}

	res, err := client.BargainProductInfo(c, &product.BargainProductInfoRequest{
		Id: req.Id,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "砍价商品详情展示", map[string]interface{}{"BargainProductInfo": res})
}
